//
//  CacheTableViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/3/8.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "CacheTableViewCell.h"

@implementation CacheTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)dealloc{
    [_str release];
    [_label1 release];
    [_label2 release];
    [super dealloc];
}

- (void)createSubView{
    
    self.label1 = [[BaseLabel alloc] init];
    [self.contentView addSubview:_label1];
    [_label1 release];
    
    
    self.label2 = [[UILabel alloc] init];
    [self.contentView addSubview:_label2];
    [_label2 release];
    
    self.imaeHeader = [[UIImageView alloc] init];
    [self.contentView addSubview:_imaeHeader];
    [_imaeHeader release];
    
  
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearCachesAction:) name:@"qinglihuanchun" object:nil];

    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Caches"];
    
    
     _label2.text = [NSString stringWithFormat:@"%.2fMB",[self floderSizeAtPath:path]];
    
    
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    _imaeHeader.frame = CGRectMake(15, 5, 40, 40);
    
    _label1.frame = CGRectMake(80, 5, 100, 40);

    _label2.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 1, 100, 50);
    _label2.font = [UIFont systemFontOfSize:16];
    _label2.textColor = [UIColor lightGrayColor];
}


- (long long)fileSizeAtPath:(NSString *)path {
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}
- (float)floderSizeAtPath:(NSString *)path {
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }else {
        NSEnumerator *enumerator = [[manager subpathsAtPath:path] objectEnumerator];
        NSString *fileName = [NSString string];
        CGFloat floderSize = 0;
        while ((fileName = [enumerator nextObject]) != nil) {
            NSString *absolutPath = [path stringByAppendingPathComponent:fileName];
            floderSize += [self fileSizeAtPath:absolutPath];
        }
        return floderSize/ 1024/ 1024;
    }

}
- (void)clearAction {
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Caches"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSArray *fileNameArr = [manager subpathsAtPath:path];
        for (NSString *fileName in fileNameArr) {
            NSString *absoltPath = [path stringByAppendingPathComponent:fileName];
            [manager removeItemAtPath:absoltPath error:nil];
        }
    }
}
- (void)clearCachesAction:(NSNotification *)notification {
    
    [self clearAction];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Caches"];
    
    
    _label2.text = [NSString stringWithFormat:@"%.2fMB",[self floderSizeAtPath:path]];
   
}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
